package ChatApp.ServerPart;
//import sun.jdbc.odbc.ee.ConnectionPool;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: Zuxs
 * Date: 4.09.12
 * Time: 21:36
 * To change this template use File | Settings | File Templates.
 */
public class UsersCeeper {

    private ArrayList<User> _users;
    private String fileName;

    public UsersCeeper(String fileName){
        //use method that fill users list
        this.fileName = fileName;
        _users = new ArrayList<User>();
//        _users.add(new User("Peeter","154.154.154.154",1154));
//        _users.add(new User("Peeter2","156.156.156.156",1156));
//        _users.add(new User("Peeter3","157.157.157.157",1157));
//        _users.add(new User("Peeter4","158.158.158.158",1158));
//        _users.add(new User("Osilius","194.195.21.194",9648));
//        _users = readUserFromFile(null);
        writeUserToFile(null,_users);

    }



    public UsersCeeper(){
        _users = readUserFromFile(null);
    }



    public ArrayList<User> readUserFromFile(File file){
        ArrayList<User> users = new ArrayList<User>();
        try{
            FileInputStream fis;
            if(file==null)
                fis = new FileInputStream(fileName);
            else
                fis = new FileInputStream(file);

            DataInputStream in = new DataInputStream(fis);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            while (true){
                String line = br.readLine();
                if(line == null || line == "" )
                    break;

                User tmpUser = fromString(line);
                   if(tmpUser!=null)
                        if(!this._users.contains(tmpUser))
                            users.add(tmpUser);

            }
            br.close();
            in.close();
            fis.close();
        }catch (FileNotFoundException e){

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        for (User u : _users){      //NOT SHURE IS THIS CHECK IS NEEDED OR NOT
            if(!users.contains(u))
                users.add(u);
        }
        return  users;
    }


    public void writeUserToFile(File file, ArrayList<User> users){

        try{
            FileOutputStream fos;
            if(file!=null)
                fos = new FileOutputStream(file);
            else
                fos = new FileOutputStream(fileName);

            DataOutputStream out = new DataOutputStream(fos);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
            if(users !=null)
                for(Object u : users){
                    User tmpU = (User) u;
                    String userInfo = tmpU.toString();
                    bw.write(userInfo);
                }

            bw.close();
            out.close();
            fos.close();
        }catch (FileNotFoundException e){

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


    }

    public void updateUsersFromFile(File file){
        ArrayList<User> user = readUserFromFile(file);
        writeUserToFile(null,user);

    }

    public File fileWithUsersAtLocalMachine(){
        File file = new File(fileName);
        writeUserToFile(file,_users);
        return file;
    }     //method that update users list


    public User fromString(String pUser) {
        String REGEX = "^([a-zA-Z]+),((([0-9]{1,3}).){3}([0-9]{1,3})),([0-9]{1,5})$"; //TO-DO:make read from config file
        Pattern pattern = Pattern.compile(REGEX);
        Matcher matcher = pattern.matcher(pUser);
        if (matcher.matches()) {
            String[] userInfo = pUser.split(",");
            String usrNm = userInfo[0];
            String usrHst = userInfo[1];
            int userPrt = Integer.parseInt(userInfo[2]);

            Socket s = null;
            try {
                s = new Socket(usrHst,userPrt);
                User usr = new User(s);
                usr.name = usrNm;
                return usr;
            } catch (IOException e) {
                return null;
            }
        }
        return null;
    }

    public void remove(User user) {
        _users.remove(user);
    }
}
